package com.lzs.Controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lzs.PO.Family;
import com.lzs.PO.Member;
import com.lzs.PO.Page;
import com.lzs.Service.MemberService;
import com.lzs.util.Msg;

@Controller
@RequestMapping(value = "/member")
public class MemberController {

	@Autowired
	private MemberService memberService;

	@ResponseBody
	@RequestMapping(value = "/member_check")
	public Msg checkMember(String people_id) {

		Boolean b = this.memberService.countMemberByPeopleId(people_id);

		if (b) {

			return Msg.success();

		} else {

			return Msg.fail();

		}
	}

	@RequestMapping(value = "/member_list")
	public String memberList(Model model, HttpServletRequest request, @RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "5") Integer pageSize, String keyword) throws Exception {

		Page<Member> page = this.memberService.findMemberPagelist(keyword, pageSize, pageNum);

		model.addAttribute("page", page);

		return "/member/member_list";
	}

	@RequestMapping(value = "/member_add")
	public String addMember(String family_id, Model model) {

		Member member = this.memberService.findMemberWithFamily(family_id);

		model.addAttribute("member", member);

		return "/member/member_add";

	}

	@ResponseBody
	@RequestMapping(value = "/member_add_action")
	public Msg addMemberAction(String member_id, String member_name, String family_id, String relation, String rural_id,
			String medical_card_id, String people_id) {

		try {

			Map<String, Object> map = new HashMap<String, Object>();

			map.put("member_id", member_id);

			map.put("member_name", member_name);

			map.put("family_id", family_id);

			map.put("relation", relation);

			map.put("rural_id", rural_id);

			map.put("medical_card_id", medical_card_id);

			map.put("people_id", people_id);
             
			this.memberService.addMember(map);

			return Msg.success();

		} catch (Exception e) {

			e.printStackTrace();

			return Msg.fail();

		}

	}
	@RequestMapping(value = "/member_update")
	public String updateMember(String family_id,Model model) {
		
		Member member = this.memberService.findMemberWithFamily(family_id);
		
		model.addAttribute("member", member);
		
		return"/member/member_update";
	}
	
	@ResponseBody
	@RequestMapping(value = "/member_update_action")
	public Msg updateMemberAction(String member_id, String member_name, String family_id, String relation, String rural_id,
			String medical_card_id, String people_id) {
		
		try {

			Map<String, Object> map = new HashMap<String, Object>();

			map.put("member_id", member_id);

			map.put("member_name", member_name);

			map.put("family_id", family_id);

			map.put("relation", relation);

			map.put("rural_id", rural_id);

			map.put("medical_card_id", medical_card_id);

			map.put("people_id", people_id);
             
			this.memberService.updateMember(map);

			return Msg.success();

		} catch (Exception e) {

			e.printStackTrace();

			return Msg.fail();

		}
		
	}
	@ResponseBody
	@RequestMapping(value = "/member_delete")
	public Msg deleteMember(String people_id) {
		
		try {
			
			this.memberService.deleteMemberByPeopleId(people_id);
			
			return Msg.success();
			
			
		}catch(Exception e) {
			
			e.printStackTrace();
			
			return Msg.fail();
			
		}
	}
}
